# print(chr(25110),chr(12455))
from PIL import Image  #图片处理模块
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('file')  #输入文件
parser.add_argument('-o','--output') #输出文件
parser.add_argument('--width',type=int,default=80) #输出字符画宽
parser.add_argument('--height',type=int,default=80) #输出字符画高

# 获取参数

args = parser.parse_args()

IMG = args.file
WIDTH = args.width
HEIGHT = args.height
OUTPUT = args.output
ascii_char = ("!@#$%^&*(_)+Qwertyugdfgiufhhfqqfuiqhfquifhqhfiuqfhdqdwqzxcnbm|'")
def get_char(r,g,b,alpha=256):
    # alpha为0时,表示该位置为空白
    if alpha==0:
        return ' '
    else:
     lenght = len(ascii_char)
     #吧RGB色域转换成灰度图，使用权重
     gray = int(r*0.7+g*0.2+b*0.1)
     unit = (256.0+1)/lenght
     return ascii_char[int((gray/unit))]

if __name__ == "__main__":
       im = Image.open(IMG)
       im = im.resize((WIDTH,HEIGHT),Image.NEAREST) 
       txt = ""
       for i in range(HEIGHT):
           for j in range(WIDTH):
               txt+=get_char(*im.getpixel((j,i)))
           txt+="\n" 
       print(txt)
if OUTPUT:
    with open(OUTPUT,'w') as f:
        f.write(txt)
else:
    with open('output.txt','w') as f:
         f.write(txt)             


      
         
